import { IBodyCreateConfig } from "../core/rpg/types";
import base_component from "../component/base_component";
import rpg_component from "../component/rpg_component";

/**
 * 创建服务类
 * 职责：
 * 1. 实体创建数据组装
 * 2. 配置数据格式化
 * 3. 数据验证和处理
 */
export class CreateService {

    /**
     * 获取创建数据
     * @param baseComponent 基础组件
     * @param rpgComponent RPG组件
     * @returns 创建配置数据
     */
    static getCreateData(baseComponent: base_component, rpgComponent: rpg_component): IBodyCreateConfig {
        return {
            url: baseComponent.show,
            emoji: baseComponent.emoji,
            name: baseComponent.nickName,
            level: 1,
            skill: JSON.parse(JSON.stringify(rpgComponent.selfSkill))
        };
    }

    /**
     * 验证创建数据
     * @param createData 创建数据
     * @returns 验证结果
     */
    static validateCreateData(createData: IBodyCreateConfig): {
        isValid: boolean;
        errors: string[];
    } {
        const errors: string[] = [];

        if (!createData.name || createData.name.trim() === '') {
            errors.push('实体名称不能为空');
        }

        if (!createData.emoji || createData.emoji.trim() === '') {
            errors.push('实体表情不能为空');
        }

        if (createData.level <= 0) {
            errors.push('实体等级必须大于0');
        }

        if (!createData.skill || createData.skill.length === 0) {
            errors.push('实体必须拥有至少一个技能');
        }

        return {
            isValid: errors.length === 0,
            errors
        };
    }

    /**
     * 格式化创建数据用于显示
     * @param createData 创建数据
     * @returns 格式化后的显示数据
     */
    static formatCreateDataForDisplay(createData: IBodyCreateConfig): {
        displayName: string;
        basicInfo: string;
        skillInfo: string;
    } {
        const displayName = `${createData.emoji} ${createData.name}`;
        const basicInfo = `等级: ${createData.level}`;
        const skillInfo = `技能数量: ${createData.skill ? createData.skill.length : 0}`;

        return {
            displayName,
            basicInfo,
            skillInfo
        };
    }

    /**
     * 克隆创建数据
     * @param createData 原始创建数据
     * @returns 克隆后的创建数据
     */
    static cloneCreateData(createData: IBodyCreateConfig): IBodyCreateConfig {
        return {
            url: createData.url,
            emoji: createData.emoji,
            name: createData.name,
            level: createData.level,
            skill: JSON.parse(JSON.stringify(createData.skill))
        };
    }

    /**
     * 合并创建数据
     * @param baseData 基础数据
     * @param additionalData 附加数据
     * @returns 合并后的创建数据
     */
    static mergeCreateData(baseData: IBodyCreateConfig, additionalData: Partial<IBodyCreateConfig>): IBodyCreateConfig {
        return {
            url: additionalData.url || baseData.url,
            emoji: additionalData.emoji || baseData.emoji,
            name: additionalData.name || baseData.name,
            level: additionalData.level || baseData.level,
            skill: additionalData.skill || baseData.skill
        };
    }

    /**
     * 创建默认配置
     * @returns 默认创建配置
     */
    static createDefaultConfig(): IBodyCreateConfig {
        return {
            url: '',
            emoji: '🤖',
            name: '默认实体',
            level: 1,
            skill: []
        };
    }

    /**
     * 获取创建数据摘要
     * @param createData 创建数据
     * @returns 数据摘要
     */
    static getCreateDataSummary(createData: IBodyCreateConfig): {
        name: string;
        level: number;
        skillCount: number;
        hasUrl: boolean;
        hasEmoji: boolean;
    } {
        return {
            name: createData.name,
            level: createData.level,
            skillCount: createData.skill ? createData.skill.length : 0,
            hasUrl: Boolean(createData.url),
            hasEmoji: Boolean(createData.emoji)
        };
    }
} 